//
//  HippyViewController.m
//  ZgcBank
//
//  Created by niuyangyang on 2023/4/27.
//

#import "HippyViewController.h"
#import "Masonry.h"
#import "HippyRootView.h"
#import "HippyLog.h"
#import "HippyBundleURLProvider.h"
#import "HippySecondViewController.h"

#define DEMO_ENABLE_TURBO 1

@interface HippyViewController ()<HippyBridgeDelegate, HippyMethodInterceptorProtocol>

@end

@implementation HippyViewController

- (void)nnnn {
  HippySecondViewController *vc = [[HippySecondViewController alloc] init];
  [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
  self.view.backgroundColor = [UIColor whiteColor];
//  [self performSelector:@selector(nnnn) withObject:nil afterDelay:5];
  
  
//  HippySetLogFunction(^(HippyLogLevel level, HippyLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
//      NSLog(@"-------------- 调试 --------------\nhippy says:%@ in file [%@:%d]", message, fileName, lineNumber.intValue);
//  });

  
  BOOL isSimulator = NO;
  #if TARGET_IPHONE_SIMULATOR
      isSimulator = YES;
  #endif
  
  //release macro below if use debug mode
//  #define HIPPYDEBUG
      
  #ifdef HIPPYDEBUG
      NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO), @"DebugMode": @(YES)};
      NSString *bundleStr = [HippyBundleURLProvider sharedInstance].bundleURLString;
      NSURL *bundleUrl = [NSURL URLWithString:bundleStr];
      HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:self
                                                        bundleURL:bundleUrl
                                                   moduleProvider:nil
                                                    launchOptions:launchOptions
                                                      executorKey:@"Demo"];
      HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge moduleName:@"Demo" initialProperties:@{@"isSimulator": @(isSimulator)} shareOptions:@{@"DebugMode": @(YES)} delegate:nil];
  #else
  
  NSLog(@"%@", NSHomeDirectory());
  
      NSString *commonBundlePath = [[NSBundle mainBundle] pathForResource:@"vendor.ios" ofType:@"js" inDirectory:@"jsbundle"];
//  NSString *commonBundlePath = [NSString stringWithFormat:@"%@/Documents/ios/vendor.ios.js", NSHomeDirectory()];
      NSString *businessBundlePath = [[NSBundle mainBundle] pathForResource:@"index.ios" ofType:@"js" inDirectory:@"jsbundle"];
//  NSString *businessBundlePath = [NSString stringWithFormat:@"%@/Documents/ios/index.ios.js", NSHomeDirectory()];
      NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO)};
      HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:self
                                                        bundleURL:[NSURL fileURLWithPath:commonBundlePath]
                                                   moduleProvider:nil
                                                    launchOptions:launchOptions
                                                      executorKey:@"Demo"];
      HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge businessURL:[NSURL fileURLWithPath:businessBundlePath] moduleName:@"Demo" initialProperties:  @{@"isSimulator": @(isSimulator)} launchOptions:nil shareOptions:nil debugMode:NO delegate:nil];
  #endif
//      bridge.methodInterceptor = self;
      
      
  //    HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:self bundleURL:[NSURL URLWithString:@"http://localhost:38989/index.bundle?platform=ios&dev=true&minify=false"] moduleProvider:nil launchOptions:nil executorKey:@"Demo"];
  //    HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge moduleName:@"Demo" initialProperties:nil shareOptions:nil delegate:nil];;
  
  [self.view addSubview:rootView];

  [rootView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view);
    make.left.equalTo(self.view);
    make.right.equalTo(self.view);
    make.bottom.equalTo(self.view);
  }];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  self.navigationController.navigationBar.hidden = YES;
}

- (BOOL)shouldStartInspector:(HippyBridge *)bridge {
    return bridge.debugMode;
}

- (NSURL *)inspectorSourceURLForBridge:(HippyBridge *)bridge {
    return bridge.bundleURL;
}

@end
